package com.it.myd.controller.main.module;

import com.it.myd.bean.box.Article;
import com.it.myd.bean.box.Mark;
import com.it.myd.bean.box.Record;
import com.it.myd.bean.main.ReturnValue;
import com.it.myd.bean.main.User;
import com.it.myd.help.StringHelper;
import com.it.myd.service.main.module.map.MapService;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpSession;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.text.SimpleDateFormat;
import java.util.List;

@Controller
@RequestMapping("/module/map")
public class MapController {

    @Autowired
    private MapService mapService;

    @RequestMapping("addMark")
    @ResponseBody
    public ReturnValue addMark(HttpServletRequest request){
        HttpSession session = request.getSession();
        User user = (User) session.getAttribute("user");
        String bj_lng = request.getParameter("bj_lng");
        String bj_lat = request.getParameter("bj_lat");
        String bj_name = request.getParameter("bj_name");
        String bj_ms = request.getParameter("bj_ms");
        String bj_qx = request.getParameter("bj_qx");
        SimpleDateFormat sdf= new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        Mark mark = new Mark();
        mark.setMk_lng(bj_lng);
        mark.setMk_lat(bj_lat);
        mark.setCreateUser(user);
        mark.setCreatetime(sdf.format(System.currentTimeMillis()));
        mark.setMk_ms(bj_ms);
        mark.setMk_name(bj_name);
        mark.setMk_purview(bj_qx);
        mapService.insertMarket(mark);
        ReturnValue returnValue = new ReturnValue();
        returnValue.isSuccess();
        return returnValue;
    }

    @RequestMapping("changeMark")
    @ResponseBody
    public ReturnValue changeMark(HttpServletRequest request){
        HttpSession session = request.getSession();
        User user = (User) session.getAttribute("user");
        String code = request.getParameter("code");
        String bj_name = request.getParameter("bj_name");
        String bj_ms = request.getParameter("bj_ms");
        String bj_qx = request.getParameter("bj_qx");
        SimpleDateFormat sdf= new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        Mark mark = new Mark();
        mark.setMk_id(Integer.parseInt(code));
        mark.setCreateUser(user);
        mark.setMk_ms(bj_ms);
        mark.setMk_name(bj_name);
        mark.setMk_purview(bj_qx);
        mapService.changeMarket(mark);
        ReturnValue returnValue = new ReturnValue();
        returnValue.isSuccess();
        return returnValue;
    }


    @RequestMapping("getMark")
    @ResponseBody
    public ReturnValue getMark(HttpServletRequest request){
        HttpSession session = request.getSession();
        User user = (User) session.getAttribute("user");
        String code = request.getParameter("code");
        String bj_lng = request.getParameter("bj_lng");
        String bj_lat = request.getParameter("bj_lat");
        String bj_name = request.getParameter("bj_name");
        String bj_ms = request.getParameter("bj_ms");
        String bj_qx = request.getParameter("bj_qx");
        String userid = request.getParameter("userid");
        SimpleDateFormat sdf= new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        Mark mark = new Mark();
        if(StringHelper.isNotNull(code)){
            mark.setMk_id(Integer.parseInt(code));
        }
        mark.setMk_lng(bj_lng);
        mark.setMk_lat(bj_lat);
        mark.setMk_ms(bj_ms);
        mark.setMk_name(bj_name);
        mark.setMk_purview(bj_qx);
        User t_user = new User();
        if(StringHelper.isNotNull(userid)){
            t_user.setUser_id(Integer.parseInt(userid));
        }else{
            t_user.setUser_id(user.getUser_id());
        }
        mark.setCreateUser(t_user);
        List<Mark> list = mapService.getMarkByMark(mark);
        ReturnValue<Mark> returnValue = new ReturnValue();
        returnValue.setRetList(list);
        returnValue.isSuccess();
        return returnValue;
    }

    @RequestMapping("deleteMark")
    @ResponseBody
    public ReturnValue deleteMark(HttpServletRequest request){
        HttpSession session = request.getSession();
        User user = (User) session.getAttribute("user");
        String mk_id = request.getParameter("code");
        Mark mark = new Mark();
        mark.setMk_id(Integer.parseInt(mk_id));
        mapService.deleteMarket(mark);
        ReturnValue returnValue = new ReturnValue();
        returnValue.isSuccess();
        return returnValue;
    }
}
